package com.itheima.web.manager;

import com.itheima.domain.db.UserInfo;
import com.itheima.service.db.UserInfoService;
import com.itheima.util.SimpleWeather;
import net.sf.json.JSONObject;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class HomeManager {


    @Reference
    private UserInfoService userInfoService;

    public Map<String, Integer> findByAge() {
        List<UserInfo> userInfoList = userInfoService.findAll();

        Integer zero_ten = 0;
        Integer eleven_twenty = 0;
        Integer twentyOne_thirty = 0;
        Integer thirtyOne_forty = 0;
        Integer fortyOne_fifty = 0;

        for (int i = 0; i < userInfoList.size(); i++) {
            if (userInfoList.get(i).getAge() >= 0 && userInfoList.get(i).getAge() <= 10) {
                zero_ten++;
            }
        }
        for (int i = 0; i < userInfoList.size(); i++) {
            if (userInfoList.get(i).getAge() >= 10 && userInfoList.get(i).getAge() <= 20) {
                eleven_twenty++;
            }
        }
        for (int i = 0; i < userInfoList.size(); i++) {
            if (userInfoList.get(i).getAge() >= 21 && userInfoList.get(i).getAge() <= 30) {
                twentyOne_thirty++;
            }
        }
        for (int i = 0; i < userInfoList.size(); i++) {
            if (userInfoList.get(i).getAge() >= 31 && userInfoList.get(i).getAge() <= 40) {
                thirtyOne_forty++;
            }
        }
        for (int i = 0; i < userInfoList.size(); i++) {
            if (userInfoList.get(i).getAge() >= 41 && userInfoList.get(i).getAge() <= 50) {
                fortyOne_fifty++;
            }
        }

        Map<String, Integer> map = new HashMap<>();
        map.put("zero_ten", zero_ten);
        map.put("eleven_twenty", eleven_twenty);
        map.put("twentyOne_thirty", twentyOne_thirty);
        map.put("thirtyOne_forty", thirtyOne_forty);
        map.put("fortyOne_fifty", fortyOne_fifty);
        return map;
    }

    public Map<String, Integer> findByGender() {
        Integer man = 0;
        Integer woman = 0;

        List<UserInfo> userInfoList = userInfoService.findAll();
        for (int i = 0; i < userInfoList.size(); i++) {
            if ("man".equals(userInfoList.get(i).getGender())) {
                man++;
            }
        }

        for (int i = 0; i < userInfoList.size(); i++) {
            if ("woman".equals(userInfoList.get(i).getGender())) {
                woman++;
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("man", man);
        map.put("woman", woman);
        return map;
    }



}
